Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

heap

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

heap

binary heap (priority queue) algorithms (ported from Python's heapq module)

  • 0.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2M
decreased by-0.77%
Maintainers
1
Weekly downloads
 
Created

What is heap?

The 'heap' npm package provides an easy-to-use implementation of the heap data structure, allowing users to manage a collection of items with operations such as insertion, removal, and retrieval of the smallest (or largest) item efficiently. It supports both min-heaps and max-heaps, making it versatile for various applications such as priority queues, sorting algorithms, and more.

What are heap's main functionalities?

Creating and using a min heap

This code demonstrates how to create a min heap, insert items into it, and then remove the smallest item. The 'heap' package automatically organizes the items so that the smallest item can be efficiently removed.

const Heap = require('heap');
let minHeap = new Heap();
minHeap.push(2);
minHeap.push(3);
minHeap.push(1);
console.log(minHeap.pop()); // Outputs: 1

Creating and using a max heap

This code shows how to create a max heap by providing a custom comparison function. Items are inserted into the heap, and the largest item is removed efficiently.

const Heap = require('heap');
let maxHeap = new Heap((a, b) => b - a);
maxHeap.push(2);
maxHeap.push(3);
maxHeap.push(1);
console.log(maxHeap.pop()); // Outputs: 3

Heapify an existing array

This example demonstrates how to transform an existing array into a heap structure in-place using the 'heapify' method. This is useful for efficiently preparing data for heap operations.

const Heap = require('heap');
let numbers = [3, 1, 2];
Heap.heapify(numbers);
console.log(numbers); // Outputs a heapified array

Other packages similar to heap

Keywords

FAQs

Package last updated on 02 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc